import Vue from 'vue'
import Vuex,{Store} from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Store({
  actions:{
    // 服务员
    addPerson(context,personObj){
      if(context.state.persons.length < 10){
        context.commit('ADD_PERSON',personObj)
      }
    },
    getTheDog({commit}){
      axios.get('https://dog.ceo/api/breed/husky/images/random').then(
        response => {
          console.log('成功',response.data.message)
          commit('SAVE_DOG',response.data.message)
        },
        error => {
          console.log('失败',error)
        }
      )
    }
  },
  mutations:{
    // 厨师
    ADD_PERSON(state,personObj){
      state.persons.unshift(personObj)
    },
    SAVE_DOG(state,dogUrl){
      state.pictures.unshift(dogUrl)
    }
  },
  state:{
    // 人列表
    persons:[
      {id:'asdytasy01',name:'静静',age:18},
      {id:'asdytasy02',name:'萌萌',age:90},
      {id:'asdytasy03',name:'老罗',age:5},
    ],
    // 图片列表
    pictures:[
      'http://49.232.112.44/images/hot.jpg',
      'http://49.232.112.44/images/cool.jpg',
    ]
  },
  getters:{}
})